using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using ENTITIES;

namespace DAL
{
  public  class MaDealDAL
    {
       private SqlConnection cn = null;
        public MaDealDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
       public void Insert (TBMaDeal md)
        {
            SqlCommand cm = new SqlCommand("MaDeal_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@HinhThucTT",md.hinhthuctt);
            cm.Parameters.AddWithValue("@MaSP", md.masp);
          
            cm.ExecuteNonQuery();
        }
        public void Update(TBMaDeal md)
        {
            SqlCommand cm = new SqlCommand("MaDeal_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDeal", md.madeal);
            cm.Parameters.AddWithValue("@HinhThucTT", md.hinhthuctt);
            cm.Parameters.AddWithValue("@MaSP", md.masp);
            
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("MaDeal_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDeal", iD);
            cm.ExecuteNonQuery();
        }

        public tbMaDealCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("MaDeal_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbMaDealCollection madealCollection = new tbMaDealCollection();

            while (dr.Read())
            {
                TBMaDeal madeal = new TBMaDeal();
                madeal.madeal = int.Parse(dr.GetValue(0).ToString());
                madeal.hinhthuctt = int.Parse(dr.GetValue(1).ToString());
                madeal.masp = int.Parse(dr.GetValue(2).ToString());

                madealCollection.Add(madeal);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return madealCollection;
        }
    }
    }

